package org.vaadinpa.gui.views;

import java.util.List;

import org.vaadinpa.data.NewsItem;
import org.vaadinpa.gui.AbstractView;
import org.vaadinpa.gui.VaadinPA;

import com.vaadin.ui.Label;
import com.vaadin.ui.Panel;
import com.vaadin.ui.themes.Reindeer;

public class NewsView extends AbstractView {

	private static final long serialVersionUID = -6022804925667919313L;

	private Panel itemsPanel = new Panel();

	public NewsView() {

		itemsPanel.setSizeFull();
		itemsPanel.setScrollable(true);
		itemsPanel.getContent().setWidth(100, UNITS_PERCENTAGE);
		itemsPanel.getContent().setHeight(null);
		itemsPanel.getContent().addStyleName(Reindeer.LAYOUT_BLACK);

		addComponent(itemsPanel);
		setSizeFull();
		super.setExpandRatio(itemsPanel, 1);
	}

	@Override
	public void reload() {

		itemsPanel.removeAllComponents();

		List<NewsItem> news = VaadinPA.getPlanet().news;
		if (news.size() > 50) {
			news = news.subList(0, 50);
		}
		for (NewsItem n : news) {

			n.read = true;

			Label l = new Label(n.produceHTML(), Label.CONTENT_XHTML);

			Panel p = new Panel();
			p.getContent().addStyleName(Reindeer.LAYOUT_BLACK);
			p.addComponent(l);
			itemsPanel.addComponent(p);
		}
	}

	@Override
	public String getTitle() {
		return "News";
	}
}
